home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2734 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.0 KB  |  59 lines

  1. Path: mail2news.demon.co.uk!hpl3sn03.cern.ch
  2. From: Dan Pop <danpop@mail.cern.ch>
  3. Newsgroups: comp.lang.c++,comp.lang.c
  4. Subject: Re: Hungarian notation
  5. Date: Fri, 19 Jan 1996 11:24:17 +0100
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <9601191024.AA27337@dxmint.cern.ch>
  8. References: <4dhkae$an9@blackice.winternet.com> <821890870snz@genesis.demon.co.uk> <30fd5306.3171520@nntp.ix.netcom.com> <9601181211.AA03705@dxmint.cern.ch> <4dnf74$1sa@fountain.mindlink.net>
  9. X-NNTP-Posting-Host: hpl3sn03.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11. X-Mail2News-Path: dxmint.cern.ch!hpl3sn03.cern.ch
  12.  
  13. genew@mindlink.bc.ca (Gene Wirchenko) writes:
  14.  
  15. >Dan Pop <danpop@mail.cern.ch> wrote:
  16. >
  17. >>The last statement (sort of) guarantees that any arithmetic type can be
  18. >>safely converted to long double, even if some information is lost in the
  19. >>conversion (I'm typing this text on a machine where both long and long
  20. >
  21. >     I don't understand this, Dan.
  22. >     "...safely converted..." and "...even if some information is lost
  23. >in the conversion..." don't go together in my understanding.
  24. >     Do you simply mean that C won't barf or that the program won't
  25. >crash?  Or what?
  26.  
  27. By "safely converted" I meant that no overflow could possibly occur,
  28. even if the conversion results in a loss of precision.  Here's an
  29. example which can be reproduced on systems using 32-bit int's and IEEE 
  30. float's:
  31.  
  32.     hpl3sn02:~/tmp 73> cat test.c
  33.     #include <stdio.h>
  34.     #include <limits.h>
  35.  
  36.     main()
  37.     {
  38.     int i = INT_MAX - 100;
  39.     float f = i;
  40.  
  41.     printf("%d %.0f\n", i, f);
  42.     return 0;
  43.     }
  44.     hpl3sn02:~/tmp 74> cc test.c
  45.     hpl3sn02:~/tmp 75> ./a.out 
  46.     2147483547 2147483520
  47.  
  48. The last bits of 'i' have been lost during the conversion, for the
  49. simple reason that 32 bits cannot be compressed into the 24 bits allocated
  50. to the mantissa of 'f'.  The conversion itself is safe and no undefined
  51. behaviour is invoked (even if the result is implementation defined).
  52.  
  53. Dan
  54. -- 
  55. Dan Pop
  56. CERN, CN Division
  57. Email: danpop@mail.cern.ch 
  58. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  59.